/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectProductInfoController
 * 产品信息表
 *
 * 修改纪录
 * 2021-08-24 版本：1.0 chengengwei 创建。
 * @version 版本：1.0
 * @author 作者：chengengwei
 * 创建日期：2021-08-24
 */

package com.topscomm.mpm.controller;

import com.topscomm.mpm.controller.auto.MpmProjectProductInfoControllerAuto;
import com.topscomm.mpm.service.impl.MpmProjectProductInfoService;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.util.ErrorLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: chengengwei
 * @date: 2021-08-24
 * @description:产品信息表Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectProductInfo")
public class MpmProjectProductInfoController extends MpmProjectProductInfoControllerAuto implements IMpmProjectProductInfoController {

	private Logger logger = LoggerFactory.getLogger(MpmProjectPolymerizationController.class);
	@Autowired
	private MpmProjectProductInfoService mpmProjectProductInfoService;

	/**
	 * @author: chengengwei
	 * @date: 2021-08-24
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: chengengwei
	 * @date: 2021-08-24
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-11-28
	 * @param paraMap
	 * @return
	 * @description:销售项目涉及产品范围json增加产品线编码
	 * @modify:
	 */
	@PostMapping("/generateProductLineCode")
	public ResponseResult<Object> generateProductLineCode(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectProductInfoService.generateProductLineCode(paraMap);
			return ResponseResult.ok("数据处理完毕");
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-11-28
	 * @param paraMap
	 * @return
	 * @description:销售项目涉及产品范围json增加产品线编码
	 * @modify:
	 */
	@GetMapping("/queryProductLineNo")
	public ResponseResult<Object> queryProductLineNo(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			List<String> res = mpmProjectProductInfoService.queryProductLineNo(paraMap);
			return ResponseResult.ok(res.stream().collect(Collectors.joining(",")));
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-11-28
	 * @param paraMap
	 * @return
	 * @description:销售项目涉及产品范围json增加产品线编码(立项变更修改)
	 * @modify:
	 */
	@PostMapping("/generateProductLineCodeChange")
	public ResponseResult<Object> generateProductLineCodeChange(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectProductInfoService.generateProductLineCodeChange(paraMap);
			return ResponseResult.ok("数据处理完毕");
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-11-28
	 * @param paraMap
	 * @return
	 * @description:销售项目涉及产品范围json增加产品线编码立项变更
	 * @modify:
	 */
	@GetMapping("/queryProductLineNoChange")
	public ResponseResult<Object> queryProductLineNoChange(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			List<String> res = mpmProjectProductInfoService.queryProductLineNoChange(paraMap);
			return ResponseResult.ok(res.stream().collect(Collectors.joining(",")));
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-04-20
	 * @param paraMap
	 * @return
	 * @description:销售项目将主表productrange字段同步到productinfo子表,将productoutrange字段同步到productoutinfo子表
	 * @modify:
	 */
	@PostMapping("/insertProductInfoSubTable")
	public ResponseResult<Object> insertProductInfoSubTable(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectProductInfoService.insertProductInfoSubTable(paraMap);
			return ResponseResult.ok("同步产品子表成功！");
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-05-16
	 * @param paraMap
	 * @return
	 * @description:销售项目同步内部外部产品历史表
	 * @modify:
	 */
	@PostMapping("/insertProductForHis")
	public ResponseResult<Object> insertProductForHis(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			mpmProjectProductInfoService.insertProductForHis(paraMap);
			return ResponseResult.ok("同步产品历史子表成功！");
		} catch (Exception ex) {
			ErrorLogUtil.writeLog(ex);
			logger.info(ex.getMessage());
			return ResponseResult.error(ex.getMessage());
		}
	}
}

